package Q530;

/**
 * 二叉搜索树的最小绝对差
 *
 * @author 23737
 * @time 2021.12.4
 */
public class Test {
    public static void main(String[] args) {

    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
   TreeNode pre;
   int MIN = Integer.MAX_VALUE;
    public int minDiffInBST(TreeNode root) {
        if (root == null) {
            return 0;
        }
        recursive(root);
        return MIN;
    }

    //递归
    private void recursive(TreeNode root) {
        if (root == null) {
            return;
        }
        recursive(root.left);
        if(pre != null){
            MIN = Math.min(MIN, root.val- pre.val);
        }
        pre = root;
        recursive(root.right);
    }
}
